package com.atguigu.srb.core.controller.api;


import com.atguigu.guigu.common.result.R;
import com.atguigu.srb.core.pojo.entity.LendItem;
import com.atguigu.srb.core.service.LendItemService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 标的出借记录表 前端控制器
 * </p>
 *
 * @author Atguigu
 * @since 2022-11-19
 */
@RestController
@RequestMapping("/api/core/lendItem")
public class ApiLendItemController {
    @Resource
    LendItemService lendItemService;
    @ApiOperation("查询投资记录")
    @GetMapping("{lendId}")
    public R list(@PathVariable("lendId")String lendId){
        List<LendItem> lendItems = lendItemService.list(Wrappers.lambdaQuery(LendItem.class)
                .in(LendItem::getStatus, Arrays.asList(1, 2))
                .eq(LendItem::getLendId , lendId)
                .orderByDesc(LendItem::getCreateTime));
        return R.ok().data("items",lendItems);
    }
    ///api/core/lendItem/notify
    @ApiOperation("投资回调")
    @PostMapping("/notify")
    public String investNotify(HttpServletRequest request){
        String result = lendItemService.investNotify(request);
        return result;
    }
    @ApiOperation("投资")
    @PostMapping("/auth/commitInvest/{investAmount}/{lendId}")
    public R commitInvest(@RequestHeader(value = "token",required = false)String token,
                          @PathVariable("investAmount") BigDecimal investAmount,
                          @PathVariable("lendId")String lendId){
        String formStr = lendItemService.commitInvest(token,investAmount,lendId);
        return R.ok().data("formStr",formStr);
    }

}

